package org.jim.server.helper;

import org.jim.core.ImStatus;
import org.jim.core.cache.redis.RedisCacheManager;
import org.jim.core.config.ImConfig;
import org.jim.core.helper.*;
import org.jim.core.helper.cs.ICsChatHelper;
import org.jim.core.helper.cs.ICsSessionHelper;
import org.jim.core.helper.im.IFriendHelper;
import org.jim.core.helper.im.IGroupHelper;
import org.jim.core.listener.ImStoreBindListener;
import org.jim.core.packets.pub.consts.ChatType;
import org.jim.core.packets.cs.session.vo.JimCsSession;
import org.jim.core.packets.cs.session.vo.JimSessionResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Redis获取持久化+同步消息助手;
 *
 * @author WChao
 * @date 2018年4月9日 下午4:39:30
 */
public class MessageHelperImpl extends AbstractHelper {

    private Logger log = LoggerFactory.getLogger(MessageHelperImpl.class);

    private ICsChatHelper csChatHelper;

    private IChatHelper chatHelper;

    private ICsSessionHelper csSessionHelper;

    private IUserHelper userHelper;

    private IGroupHelper groupHelper;

    private IFriendHelper friendHelper;

    public MessageHelperImpl() {
        this.imConfig = ImConfig.Global.get();
    }

    public MessageHelperImpl(IUserHelper userHelper, IGroupHelper groupHelper, IFriendHelper friendHelper, ICsChatHelper csChatHelper,
                             ICsSessionHelper csSessionHelper) {
        this.imConfig = ImConfig.Global.get();
        this.userHelper = userHelper;
        this.groupHelper = groupHelper;
        this.friendHelper = friendHelper;
        this.csChatHelper = csChatHelper;
        this.csSessionHelper = csSessionHelper;
    }

    @Override
    public ImStoreBindListener getBindListener() {
        return new RedisImStoreBindListener(imConfig, this);
    }

    @Override
    public ICsChatHelper getCsChatHelper() {
        return this.csChatHelper;
    }

    @Override
    public ICsSessionHelper getCsSessionHelper() {
        return this.csSessionHelper;
    }


    @Override
    public IChatHelper getChatHelper() {
        return chatHelper;
    }


    @Override
    public IUserHelper getUserHelper() {
        return this.userHelper;
    }

    @Override
    public IGroupHelper getGroupHelper() {
        return this.groupHelper;
    }

    @Override
    public IFriendHelper getFriendHelper() {
        return this.friendHelper;
    }


    /**
     * 返回Session,如果session返回null,表示无效
     *
     * @param sessionId
     * @param chatType
     */
    public JimSessionResp getSession(String sessionId, int chatType) {
        JimSessionResp jimSession = new JimSessionResp();
        jimSession.setCode(ImStatus.C_OK.getCode());
        //im聊天
        if (ChatType.CHAT_TYPE_PRIVATE.getNumber() == chatType || ChatType.CHAT_TYPE_PUBLIC.getNumber() == chatType) {

        }
        //客服聊天模式
        else if (ChatType.CHAT_TYPE_CS.getNumber() == chatType) {
            JimCsSession sessionResp = this.csSessionHelper.get(sessionId);
            if (sessionResp == null) {
                jimSession.setCode(ImStatus.C_FAIL.getCode());
                jimSession.setMsg("invalid session");
            } else {
                jimSession.setFromUserId(sessionResp.getUserId());
                jimSession.setToUserId(sessionResp.getCsId());
                jimSession.setCsTenantId(sessionResp.getCsTenantId());
                jimSession.setCsTenantLogo(sessionResp.getCsTenantLogo());
                jimSession.setCsTenantName(sessionResp.getCsTenantName());
                jimSession.setCsTenantCate(sessionResp.getCsTenantCate());
                jimSession.setFromUserName(sessionResp.getUserNick());
                jimSession.setToUserName(sessionResp.getCsNick());
            }
        }
        return jimSession;
    }


    static {
        RedisCacheManager.register(USER, Integer.MAX_VALUE, Integer.MAX_VALUE);
        RedisCacheManager.register(GROUP, Integer.MAX_VALUE, Integer.MAX_VALUE);
        RedisCacheManager.register(STORE, Integer.MAX_VALUE, Integer.MAX_VALUE);
        RedisCacheManager.register(PUSH, Integer.MAX_VALUE, Integer.MAX_VALUE);

        RedisCacheManager.register(SESSION_CS, Integer.MAX_VALUE, Integer.MAX_VALUE);
        RedisCacheManager.register(SESSION_IM, Integer.MAX_VALUE, Integer.MAX_VALUE);
    }

}
